//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


package com.interactionsource.output.midi;
import java.util.Calendar;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;

/*      Comandos MIDI utilizados na classe
 * 		ShortMessage.NOTE_OFF;
 *		ShortMessage.NOTE_ON;
 *		ShortMessage.CONTROL_CHANGE;
 *
 * Comando para envio de mensagens MIDI
 *     setMessage(int command, int channel, int data1, int data2)
 * throws InvalidMidiDataException
 * Sets the short message parameters for a channel message 
 * which takes up to two data bytes. If the message only takes one 
 * data byte, the second data byte is ignored; if the message does not 
 * take any data bytes, both data bytes are ignored. 
 *
 * Parameters:
 * command - the MIDI command represented by this message
 * channel - the channel associated with the message
 * data1 - the first data byte - Pitch
 * data2 - the second data byte - Velocity
 * Throws: 
 * InvalidMidiDataException - if the status byte or all data bytes belonging to the 
 *                            message, do not specify a valid MIDI message 
 * InvalidMidiDataException
 * 
 * Singleton		
 * 
 */
public class DispositivoMIDIOut {
		
	private MidiDevice dispositivoMIDI;
	
	private Receiver receptorMsgMIDI;
		
	public DispositivoMIDIOut(MidiDevice dispositivoMIDI) {
		// Exists only to defeat instantiation.
		try {
			
			this.dispositivoMIDI = dispositivoMIDI;		

			if (this.dispositivoMIDI.getMaxReceivers() == -1) {
				receptorMsgMIDI = dispositivoMIDI.getReceiver();

				this.dispositivoMIDI.open();
				
				System.out.println("DispositivoMIDIOut - Abrindo dispositivo: " + 
						this.dispositivoMIDI.getDeviceInfo().getName());
			}
			
			
		} catch (MidiUnavailableException e) {

			System.out.println("GerenciadorMIDI - Dispositivo MIDI nao Disponivel");

			
		} 		
		
	}	
	
	public void close() {
		dispositivoMIDI.close();
		
	}
	
	public MidiDevice getDispositivoMIDI() {
		return dispositivoMIDI;
	}

	public void setDispositivoMIDI(MidiDevice dispositivoMIDI) {
		this.dispositivoMIDI = dispositivoMIDI;
	}

	public void enviaMsgAgora(ShortMessage msg) {
		
		if ( this.dispositivoMIDI.isOpen() ) {
				
			java.util.Date now = Calendar.getInstance().getTime();
			
			long timeStamp = now.getTime();
			
			this.receptorMsgMIDI.send(msg, timeStamp);
			
			/*
			System.out.println("DispositivoMIDIOut - Enviando MSG MIDI: " + 
					" Canal: " + msg.getChannel() +
					" Comando: " + msg.getCommand() +
					" Data 1: " + msg.getData1() +
					" Data 2: " + msg.getData2() + 
					" Timestamp: " + timeStamp
					);		
			*/	
		}
							
	}
	
	public String getDispositivoMIDIName() {
		return dispositivoMIDI.getDeviceInfo().getName();
	}

	
}
